﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmShiftInfo : Form
    {
        ShiftInfo _ShiftInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        ShiftClient wcfShift;
        bool isChange = false;
        public frmShiftInfo(int ShiftID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            LoadLanguage();
            dlgResult = DialogResult.Cancel;
            _ShiftInfo = new ShiftInfo();
            LoadBranch();

            wcfShift = Service.Shift();
            _ShiftInfo.ID = ShiftID;

            if (ShiftID > 0)
            {
                _ShiftInfo = wcfShift.Shift_GetInfo(ShiftID);
                Info2Form();
            }
            listRole = _listRole;
        }

        private void LoadBranch()
        {
            BranchClient _wcfBranch = Service.Branch();
            cboBranch.DisplayMember = "Branch_Name";
            cboBranch.ValueMember = "Branch_ID";
            if (!Program.isAdmin && !Program.isViewAll)
            {
                cboBranch.DataSource = _wcfBranch.Branch_GetOne(Program.intBranchID).Tables[0];
            }
            else
                cboBranch.DataSource = _wcfBranch.Branch_GetAll().Tables[0];

        }

        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }



        void Info2Form()
        {
            if (_ShiftInfo.DepartmentID > 0)
                cboDepartment.SelectedValue = _ShiftInfo.DepartmentID;
            else
                cboDepartment.Enabled = false;

            txtShiftName.Text = _ShiftInfo.Name;
            txtTimeIn.Text = _ShiftInfo.TimeIn;
            if (_ShiftInfo.TimeIn.Trim().Length == 0)
                chkDayOff.Checked = true;
            txtTimeOut.Text = _ShiftInfo.TimeOut;
            txtTimeInB.Text = _ShiftInfo.TimeInB;
            txtTimeOutB.Text = _ShiftInfo.TimeOutB;
            if (_ShiftInfo.TimeInB.Trim().Length > 0)
                chkBreakShift.Checked = true;
            txtShift_Description.Text = _ShiftInfo.Description;
            isChange = false;
        }

        void Form2Info()
        {
            _ShiftInfo.DepartmentID = Convert.ToInt32(cboDepartment.SelectedValue);
            _ShiftInfo.Name = txtShiftName.Text.Trim();
            if (txtTimeIn.Text.Trim() == ":")
            {
                _ShiftInfo.TimeIn = "";
                _ShiftInfo.DepartmentID = 0;
            }
            else
                _ShiftInfo.TimeIn = txtTimeIn.Text.Trim();

            if (txtTimeOut.Text.Trim() == ":")
                _ShiftInfo.TimeOut = "";
            else
                _ShiftInfo.TimeOut = txtTimeOut.Text.Trim();

            if (txtTimeInB.Text.Trim() == ":")
                _ShiftInfo.TimeInB = "";
            else
                _ShiftInfo.TimeInB = txtTimeInB.Text.Trim();
            if (txtTimeOutB.Text.Trim() == ":")
                _ShiftInfo.TimeOutB = "";
            else
                _ShiftInfo.TimeOutB = txtTimeOutB.Text.Trim();
            _ShiftInfo.Description = txtShift_Description.Text.Trim();
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (txtShiftName.Text.Trim().Length == 0)
            {
                txtShiftName.Focus();
                MessageBox.Show(UpdateUI.GetString("chuanhaptencalamviec"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!chkDayOff.Checked)
            {
                if (txtTimeIn.Text.Trim() == ":")
                {
                    txtTimeIn.Focus();
                    MessageBox.Show(UpdateUI.GetString("chuanhapthoigianvao"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (txtTimeOut.Text.Trim() == ":")
                {
                    txtTimeOut.Focus();
                    MessageBox.Show(UpdateUI.GetString("chuanhapthoigianra"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            if (chkBreakShift.Checked)
            {
                if (txtTimeInB.Text.Trim() == ":")
                {
                    txtTimeIn.Focus();
                    MessageBox.Show(UpdateUI.GetString("chuanhapthoigianvao"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (txtTimeOutB.Text.Trim() == ":")
                {
                    txtTimeOut.Focus();
                    MessageBox.Show(UpdateUI.GetString("chuanhapthoigianra"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            ShiftClient wcfShift = Service.Shift();

            Form2Info();
            if (_ShiftInfo.ID > 0)//update
            {
                if (!listRole.Update)
                {
                    UISetting.MesNotRightEdit();
                    return;
                }
                if (wcfShift.Shift_Update(_ShiftInfo))
                {
                    UISetting.MesUpdateSuccesful();
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    UISetting.MesNotUpdate();
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    UISetting.MesNotRightNew();
                    return;
                }
                _ShiftInfo.ID = wcfShift.Shift_Insert(_ShiftInfo);
                if (_ShiftInfo.ID > 0)
                {
                    UISetting.MesCreateSuccesful();
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    UISetting.MesNotCreate();
                    isChange = true;
                }
            }

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_ShiftInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            cboDepartment.Enabled = true;
            _ShiftInfo = new ShiftInfo();
            _ShiftInfo.ID = 0;
            txtShiftName.Text = "";
            txtTimeIn.Text = "";
            txtTimeOut.Text = "";
            txtShift_Description.Text = "";

        }

        private void frmShiftInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show("Dữ liệu đã thay đôi.\nBạn có lưu lại trước khi thoát không?", UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            wcfShift.Close();
            this.DialogResult = dlgResult;
        }

        private void frmShiftInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void chkShift_CheckedChanged(object sender, EventArgs e)
        {
            txtTimeInB.Enabled = txtTimeOutB.Enabled = chkBreakShift.Checked;
            if (chkBreakShift.Checked)
            {
                chkDayOff.Checked = false;
            }
            else
            {
                txtTimeInB.Text = txtTimeOutB.Text = "";
            }
        }

        private void chkDayOff_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDayOff.Checked)
            {
                chkBreakShift.Checked = false;
                txtTimeIn.Text = txtTimeOut.Text = txtTimeInB.Text = txtTimeOutB.Text = "";
            }
            txtTimeIn.Enabled = txtTimeOut.Enabled = txtTimeInB.Enabled = txtTimeOutB.Enabled = !chkDayOff.Checked;
        }

        private void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            UISetting.LoadDepartment(ref cboDepartment, Convert.ToInt32(cboBranch.SelectedValue));
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

    }
}
